<template>
  <div>
    <h3>发表评论</h3>
    <hr>
    <textarea name maxlength="120" placeholder="请输入评论内容，不超过120个字" v-model="msg"></textarea>
    <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>
    <div class="cmt-list">
      <div class="cmt-item" v-for="(item, index) in commmtes" :key="index">
        <div
          class="cmt-title"
        >第{{index+1}}楼:&nbsp;&nbsp;用户:{{item.user_name}}&nbsp;&nbsp;发表时间:{{item.add_time|timeFormat}}</div>
        <div class="cmt-body">{{item.content?item.content:'此用户很懒，什么都没说'}}</div>
      </div>
    </div>
    <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>
  </div>
</template>
<script>
// import { Toast } from "mint-ui";
export default {
  data() {
    return {
      pageindex: 1,
      commmtes: [],
      msg: ""
    };
  },
  props: ["id"],
  methods: {
    getComment(id, pageindex) {
      this.$http
        .get("api/getcomments/" + id + "?pageindex=" + pageindex)
        .then(function(res) {
          if (res.body.status == 0) {
            this.commmtes = this.commmtes.concat(res.body.message);
          }
        });
    },
    getMore() {
      this.pageindex++;
      this.getComment(this.id, this.pageindex);
    },
    postComment() {
      if (this.msg.trim() == 0) {
        return 
      }
      this.$http
        .post("api/postcomment/" + this.id, { content: this.msg })
        .then(function(res) {
          // res.body.status==0说明服务器那边保存了该数据
          if (res.body.status == 0) {
          
            this.commmtes.unshift({
              user_name: "匿名用户",
              add_time: Date.now(),
              content: this.msg
            });
            this.msg = "";
          }
        });
    }
  },
  created() {
    this.getComment(this.id, this.pageindex);
  }
};
</script>   
<style lang="scss" scoped>
textarea {
  height: 100px;
  font-size: 12px;
}
.cmt-list {
  font-size: 13px;
  margin-top: 10px;
  .cmt-item {
    .cmt-title {
      background: #ccc;
      line-height: 35px;
    }
    .cmt-body {
      text-indent: 2em;
    }
  }
}
.mint-button--danger.is-plain {
  margin: 20px 0;
}
</style>
